package com.incross.itracker.util;


import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;



//참조한 문서
//http://stackoverflow.com/questions/13389870/android-4-2-broke-my-aes-encrypt-decrypt-code
public class BKStringSecureUtil {

	private static volatile BKStringSecureUtil INSTANCE;

	final static String secretKey = "12345678901234567890123456789012"; // 32bit

	static String IV = ""; // 16bit

	public static BKStringSecureUtil getInstance() {
		if(INSTANCE == null) {
			synchronized (BKStringSecureUtil.class) {
				if(INSTANCE == null) INSTANCE = new BKStringSecureUtil();
			}
		}
		return INSTANCE;
	}

	private BKStringSecureUtil() {
		IV = secretKey.substring(0, 16);
	}

	// 암호화
	public static String AES_Encode(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException,
		NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
		BadPaddingException {
		byte[] keyData = secretKey.getBytes();

		SecretKey secureKey = new SecretKeySpec(keyData, "AES");

		Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
		c.init(Cipher.ENCRYPT_MODE, secureKey, new IvParameterSpec(IV.getBytes()));

		byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
		String enStr = new String(Base64.encodeToString(encrypted, Base64.DEFAULT));

		return enStr;
	}

	// 복호화
//	public static String AES_Decode(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException,
//		NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
//		BadPaddingException {
//		byte[] keyData = secretKey.getBytes();
//		SecretKey secureKey = new SecretKeySpec(keyData, "AES");
//		Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
//		c.init(Cipher.DECRYPT_MODE, secureKey, new IvParameterSpec(IV.getBytes("UTF-8")));
//
//		byte[] byteStr = Base64.decodeBase64(str.getBytes());
//
//		return new String(c.doFinal(byteStr), "UTF-8");
//	}
}